package com.phone.modules.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phone.modules.pms.model.PmsProduct;
import com.phone.modules.pms.service.PmsProductService;
import com.phone.modules.rms.dto.RmsGameResultDTO;
import com.phone.modules.rms.dto.RmsQueryDTO;
import com.phone.modules.rms.model.RmsGame;
import com.phone.modules.rms.mapper.RmsGameMapper;
import com.phone.modules.rms.service.RmsGameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 游戏帧率排行表 服务实现类
 * </p>
 *
 * @author WangHao
 * @since 2022-04-18
 */
@Service
public class RmsGameServiceImpl extends ServiceImpl<RmsGameMapper, RmsGame> implements RmsGameService {

    @Autowired
    PmsProductService productService;

    @Autowired
    RmsGameMapper gameMapper;

    /**
     * 前台展示-检索游戏帧率
     * @param queryDTO
     * @return
     */
    @Override
    public Page searchList(RmsQueryDTO queryDTO) {
        List<RmsGameResultDTO> resultDTOList = gameMapper.searchList(queryDTO);
        if (Objects.equals(queryDTO.getOrder(), "tab1")) {
            // 如果是tab1，需要对原神最高画质帧率进行排序，逆序
            resultDTOList.sort(Comparator.comparing(RmsGameResultDTO::getGenshinFps).reversed());
            for (RmsGameResultDTO gameResultDTO : resultDTOList) {
                gameResultDTO.setPer(resultDTOList.get(0).getGenshinFps());
            }
        } else if (Objects.equals(queryDTO.getOrder(), "tab2")) {
            // 如果是tab2，需要对半小时原神整机平均功耗进行排序，正序
            resultDTOList.sort(Comparator.comparing(RmsGameResultDTO::getGenshinConsumption));
            for (RmsGameResultDTO gameResultDTO : resultDTOList) {
                gameResultDTO.setPer(resultDTOList.get(resultDTOList.size()-1).getGenshinConsumption());
            }
        } else if (Objects.equals(queryDTO.getOrder(), "tab3")) {
            // 如果是tab3，需要对和平精英流畅画质+90帧的帧率进行排序，逆序
            resultDTOList.sort(Comparator.comparing(RmsGameResultDTO::getHpFps).reversed());
            for (RmsGameResultDTO gameResultDTO : resultDTOList) {
                gameResultDTO.setPer(resultDTOList.get(0).getHpFps());
            }
        } else if (Objects.equals(queryDTO.getOrder(), "tab4")) {
            // 如果是tab4，需要对和平精英流畅画质+90帧的功耗进行排序，正序
            resultDTOList.sort(Comparator.comparing(RmsGameResultDTO::getHpConsumption));
            for (RmsGameResultDTO gameResultDTO : resultDTOList) {
                gameResultDTO.setPer(resultDTOList.get(resultDTOList.size()-1).getHpConsumption());
            }
        }
        Page page = new Page(queryDTO.getPageNum(), queryDTO.getPageSize());
        page.setRecords(resultDTOList);
        page.setTotal(gameMapper.getCount(queryDTO));
        return page;
    }

    /**
     * 分页获取游戏帧率信息列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page listGame(String keyword, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<RmsGame> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.lambda().like(RmsGame::getProductName, keyword);
        }
        // 排序
        queryWrapper.lambda().orderByDesc(RmsGame::getGenshinFps);
        return this.page(page, queryWrapper);
    }

    /**
     * 新增游戏帧率信息
     * @param game
     * @return
     */
    @Override
    public boolean saveGame(RmsGame game) {
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PmsProduct::getProductName, game.getProductName());
        PmsProduct product = productService.getOne(queryWrapper);
        if (product != null) {
            game.setProductId(product.getId());
        }
        return this.save(game);
    }

    /**
     * 更新游戏帧率信息
     * @param id
     * @param game
     * @return
     */
    @Override
    public boolean updateGame(Long id, RmsGame game) {
        game.setId(id);
        return this.updateById(game);
    }

    /**
     * 批量删除游戏帧率信息
     * @param ids
     * @return
     */
    @Override
    public boolean deleteGame(List<Long> ids) {
        return removeByIds(ids);
    }


}
